package cn.edu.scau.cmi.dengyu.client;

import java.util.Iterator;
import java.util.Set;
import cn.edu.scau.cmi.dengyu.singleton.Marshal;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;

public class MultipletonPane extends Application{
	private static TextArea taDescription = new TextArea();
	private static Set<Marshal> team;
	Stage stage = new Stage();

	static {
		team =Marshal.addInstance("朱德");
		Marshal.addInstance("彭德怀");
		Marshal.addInstance("林彪");
		Marshal.addInstance("刘伯承");
		Marshal.addInstance("贺龙");
		Marshal.addInstance("陈毅");
		Marshal.addInstance("罗荣桓");
		Marshal.addInstance("徐向前");
		Marshal.addInstance("聂荣臻");
		Marshal.addInstance("叶剑英");
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		BorderPane pane = new BorderPane();
		pane.setCenter(getHBox());
		pane.setBottom(taDescription);
		Scene scene = new Scene(pane, 800, 400);
		primaryStage.setTitle("实验一 多例模式（十大元帅）");
		primaryStage.setScene(scene);
		primaryStage.show();
		
	}
	
	private HBox getHBox(){
		HBox hBox = new HBox(15);
		hBox.setPadding(new Insets(15,5,5,5));
		Label lb = new Label("请输入要创建的元帅名称：");
		TextField tf = new TextField();
		
		Button btMarshal = new Button("创建元帅");
		
		btMarshal.setOnAction(e -> setDisplayText(tf.getText()));
		hBox.getChildren().addAll(lb, tf, btMarshal);
		return hBox;
	}
	
	private static String out(Set<Marshal> team) {
		String allName = "";
		Iterator<Marshal> teamIterator = team.iterator();
		while(teamIterator.hasNext()) {
			allName = allName + teamIterator.next().getName() + " ";
		}
		return allName;
	}
	
	private void setDisplayText(String str) {
		Marshal.addInstance(str);
		taDescription.setText(out(team) + " 十大元帅已经存在\n" + str + "不能成为元帅");
	}
	

	public static void main(String[] args) {
		Application.launch(args);
	}

	public void showWindow() throws Exception {
		start(stage);		
	}
}

